﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MasterLibrary.IO.XML;
using MasterLibrary.Logging;
using MasterLibrary.Windows.Forms;

namespace HelperLibrary
{
    public static class Globals
    {
        private static ApplicationLogger logger = ApplicationLogger.Singleton();

        public static string BackupPath = Application.UserAppDataPath + @"\backupdata.xml";
        public static string ConfigurationPath = Application.UserAppDataPath + @"\configuration.xml";

        public static Configuration appConfig = LoadConfiguration();

        public static bool SaveConfiguration(Configuration Container)
        {
            try
            {
                XmlHelper.Serialize<Configuration>(Globals.ConfigurationPath, Container);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static Configuration LoadConfiguration()
        {
            Configuration Container;

            try
            {
                XmlHelper.Deserialize<Configuration>(Globals.ConfigurationPath, out Container);
            }
            catch
            {
                try
                {
                    bool result = GetOldConfiguration();

                    if (result)
                    {
                        XmlHelper.Deserialize<Configuration>(Globals.ConfigurationPath, out Container);
                    }
                    else
                    {
                        Container = new Configuration();
                    }
                }
                catch
                {
                    Container = new Configuration();
                }
            }
            
            return Container;
        }

        public static void ShowExceptionForm(Exception ex, Form actualForm)
        {
            actualForm.Hide();

            ExceptionClass.SmtpHost = "mail.camelot-group.de";
            ExceptionClass.Recipients.Add("swa@camelot-itlab.com");
            ExceptionClass.SenderAddress = "prometheus@camelot-itlab.com";
            ExceptionClass.Subject = "Prometheus Exception occured";

            logger.Log(1, ex.Source, ex.Message);

            DialogResult result = ExceptionClass.ShowExceptionDialog(ex, "Ein unerwarteter Fehler ist aufgetreten.", "Während der Ausführung ist ein unerwarteter Fehler ausgelöst worden.");

            if (result != DialogResult.OK)
            {
                if (result == DialogResult.Abort)
                {
                    Application.Exit();
                }
                if (result == DialogResult.Retry)
                {
                    Application.Restart();
                }
            }
            else
            {
                actualForm.Show();
            }
        }

        public static bool GetOldConfiguration()
        {
            DirectoryInfo dir = new DirectoryInfo(Application.UserAppDataPath.Substring(0, Application.UserAppDataPath.LastIndexOf('\\')));
            DirectoryInfo[] versions = dir.GetDirectories();

            FileInfo backupData = new FileInfo(BackupPath);
            FileInfo configData = new FileInfo(ConfigurationPath);

            if (versions.Length >= 2)
            {
                try
                {
                    if (File.Exists(dir.FullName + "\\" + versions[versions.Length - 2] + "\\" + backupData.Name))
                    {
                        File.Copy(dir.FullName + "\\" + versions[versions.Length - 2] + "\\" + backupData.Name, Application.UserAppDataPath + "\\" + backupData.Name);
                    }
                    if (File.Exists(dir.FullName + "\\" + versions[versions.Length - 2] + "\\" + configData.Name))
                    {
                        File.Copy(dir.FullName + "\\" + versions[versions.Length - 2] + "\\" + configData.Name, Application.UserAppDataPath + "\\" + configData.Name);
                    }
                }
                catch (Exception e)
                {
                    return false;
                }
            }

            try
            {
                foreach (DirectoryInfo val in versions)
                {
                    if (val.FullName != Application.UserAppDataPath)
                    {
                        Directory.Delete(val.FullName, true);
                    }
                }
            }
            catch { }

            return true;
        }
    }
}
